package com.googlecode.sparda.commons.swing;

import javax.swing.table.TableModel;

/**
 * A class that simplify the managing of an {@link javax.swing.table.AbstractTableModel},
 * using the {@link ColumnNameToMethodCollection} composed by {@link ColumnNameToMethod}.
 * @author Fabio Grucci (<i>alias</i> <b>Phoenix</b>)
 *
 * @param <T> The type of element managed by the {@link TableModel}
 */
public abstract class AbstractTableModel<T> extends javax.swing.table.AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5953664449989042363L;
	/**
	 * The {@link ColumnNameToMethodCollection} that map the any column name to
	 * corresponding method.
	 */
	private final ColumnNameToMethodCollection<T> columnNameToMethodCollection;
	/**
	 * Create a new {@link AbstractTableModel}.
	 */
	public AbstractTableModel() {
		this.columnNameToMethodCollection = this.getColumnNameToMethodCollection();
	}
	/**
	 * Method that return the {@link ColumnNameToMethodCollection}.
	 * @return Return the {@link ColumnNameToMethodCollection}
	 */
	protected abstract ColumnNameToMethodCollection<T> getColumnNameToMethodCollection();
	/**
	 * Return the element represented in the row.
	 * Override this method.
	 * @param row The row
	 * @return The element represented in the row
	 */
	protected abstract T getElementAt(int row);
	/*
	 * (non-Javadoc)
	 * @see javax.swing.table.AbstractTableModel#getColumnName(int)
	 */
	@Override
	public String getColumnName(int column) {
		return this.columnNameToMethodCollection.getColumnName(column);
	}
	/*
	 * (non-Javadoc)
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return this.columnNameToMethodCollection.size();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return this.columnNameToMethodCollection.getColumnNameToMethod(columnIndex).getValue(this.getElementAt(rowIndex));
	}
	/*
	 * (non-Javadoc)
	 * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
	 */
	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		final ColumnNameToMethod<T> columnNameToMethod = this.columnNameToMethodCollection.getColumnNameToMethod(columnIndex);
		columnNameToMethod.setValue(this.getElementAt(rowIndex), aValue);
	}

}
